//
//  LoginViewController.m
//  Fantasia
//
//  Created by kongkong on 16/9/17.
//  Copyright © 2016年 Kongkong. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()
@property(nonatomic, strong) UIImageView *logoView;
@property(nonatomic, strong) UITextField *uidInput;
@property(nonatomic, strong) UITextField *pwdInput;
@property(nonatomic, strong) UIButton *loginButton;
@end

@implementation LoginViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        self.fanNavigationBarHidden = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _loadContent];
#ifdef DEBUG
    [self testData];
#endif
}

- (void)testData {
    self.uidInput.text = @"tangbao";
    self.pwdInput.text = @"123456";
}

- (void)_loadContent {
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.logoView];

    UILabel *uidLabel = [self labelWithText:@"用\t户\t名:" frame:CGRectMake(0, self.logoView.bottom + 30, self.view.width / 3, 25)];
    [self.view addSubview:uidLabel];
    self.uidInput.frame = CGRectMake(uidLabel.right + 20, uidLabel.top, self.view.width / 3 * 2 - 80, uidLabel.height);
    [self.view addSubview:self.uidInput];

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(self.uidInput.left, uidLabel.bottom + 5, self.uidInput.width, 0.5)];
    line.backgroundColor = [UIColor grayColor];
    [self.view addSubview:line];

    UILabel *pwdLabel = [self labelWithText:@"密\t\t码:" frame:CGRectMake(0, uidLabel.bottom + 30, self.view.width / 3, 25)];
    [self.view addSubview:pwdLabel];
    self.pwdInput.frame = CGRectMake(pwdLabel.right + 20, pwdLabel.top, self.view.width / 3 * 2 - 80, pwdLabel.height);
    [self.view addSubview:self.pwdInput];

    UIView *line2 = [[UIView alloc] initWithFrame:line.frame];
    line2.top = pwdLabel.bottom + 5;
    line2.backgroundColor = [UIColor grayColor];
    [self.view addSubview:line2];

    [self.uidInput resignFirstResponder];

    self.loginButton.top = line2.bottom + 30;
    [self.view addSubview:self.loginButton];

    [self.loginButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
}

- (UIImageView *)logoView {
    if (!_logoView) {
        NSString *imageName = [[[[NSBundle mainBundle] infoDictionary] valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
        UIImage *iconImage = [UIImage imageNamed:imageName];

        _logoView = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.width - iconImage.size.width) / 2, 100, iconImage.size.width, iconImage.size.height)];

        _logoView.image = iconImage;

    }
    return _logoView;
}

- (UITextField *)uidInput {
    if (!_uidInput) {
        _uidInput = [[UITextField alloc] init];
        _uidInput.keyboardType = UIKeyboardTypeASCIICapable;
        _uidInput.keyboardAppearance = UIKeyboardAppearanceDark;
        _uidInput.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _uidInput.autocorrectionType = UITextAutocorrectionTypeNo;
    }
    return _uidInput;
}

- (UITextField *)pwdInput {
    if (!_pwdInput) {
        _pwdInput = [[UITextField alloc] init];
        _pwdInput.keyboardType = UIKeyboardTypeASCIICapable;
        _pwdInput.keyboardAppearance = UIKeyboardAppearanceDark;
        _pwdInput.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _pwdInput.autocorrectionType = UITextAutocorrectionTypeNo;
        _pwdInput.secureTextEntry = YES;
    }
    return _pwdInput;
}

- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.width / 6, 0, self.view.width / 3 * 2, 35)];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_loginButton setBackgroundImage:[UIImage createImageWithColor:[UIColor orangeColor]] forState:UIControlStateNormal];
        [_loginButton setBackgroundImage:[UIImage createImageWithColor:[UIColor lightGrayColor]] forState:UIControlStateHighlighted];
    }
    return _loginButton;
}

- (UILabel *)labelWithText:(NSString *)text frame:(CGRect)frame {
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.textColor = [UIColor grayColor];
    label.font = [UIFont systemFontOfSize:18];
    label.textAlignment = NSTextAlignmentRight;
    return label;
}

- (void)loginAction {
    if ([self.uidInput.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"请输入用户名"];
        return;
    }
    if ([self.pwdInput.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"请输入密码"];
        return;
    }

    __weak typeof(self) weakSelf = self;
    [[[KKRequestEngine api:@"user" action:@"LOGIN"]
            parameter:@{@"uid": self.uidInput.text, @"pwd": [self.pwdInput.text md5]}]
            async:^(id<KKResponseProtocol> response, id result) {
                if ([response isSuccess]){
                    FanUser *user = [FanUser instance];
                    user.token = response.originResult[@"token"];
                    user.uid = response.originResult[@"uid"];
                    user.uname = response.originResult[@"uname"];

                    [[KKRequestEngine instance] setCustomerHead:user.token forKey:@"kk_token"];
                    [[FantasiaStorage instance]setMemoryValue:user forKey:@"user"];

                    [MBProgressHUD showSuccess:[NSString stringWithFormat:@"欢迎归来：%@",user.uname]];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        __strong typeof(weakSelf) strongSelf = weakSelf;
                        [strongSelf.rootNavigationController popViewControllerAnimated:YES];
                    });
                }
                else{
                    [response showError];
                }
    }];
}

@end
